package com.eastfair.venuebooking.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.ProjectMapper;
import com.eastfair.venuebooking.entity.Project;
import com.eastfair.venuebooking.entity.ProjectExhibition;
import com.eastfair.venuebooking.service.ProjectExhibitionService;
import com.eastfair.venuebooking.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
/**
 * <p>
 * 业务实现类
 * 
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ProjectServiceImpl extends SuperServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Resource
    private ProjectExhibitionService projectExhibitionService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<Project> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(Project model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        //雪花ID
        return R.successDef();
    }


    @Override
    public void updateExhibitionSum(Long exhibitionProjectId) {
        QueryWrapper<ProjectExhibition> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProjectExhibition::getExhibitionProjectId,exhibitionProjectId)
                .eq(ProjectExhibition::getIsDeleted,BusinessConstant.NO)
                .exists("select 1 from vb_exhibition where vb_exhibition.id=vb_project_exhibition.exhibition_id and vb_exhibition.is_deleted=0");
        int count = projectExhibitionService.count(queryWrapper);
        baseMapper.updateById(Project.builder().id(exhibitionProjectId).exhibitionSum(count).build());
    }
}
